from dataclasses import asdict

from starlette.requests import Request
from starlette.responses import JSONResponse

from .customer_exception import CustomerException
from ..models.dtos.response_result import ResponseResult
from ..models.enums.app_http_code_enum import AppHttpCodeEnum

"""
Description:全局异常处理器
Note: 1. 这里的返回体JSONResponse的第二个参数是网络状态码 2. 返回值类型一定是JSONResponse不然FastApi会继续向后传递
"""

async def customer_exception_handler(request: Request, exc: CustomerException) -> JSONResponse:
    response = ResponseResult.error_result_with_enums(exc.get_app_http_code_enum())
    return JSONResponse(content=asdict(response), status_code=response.code)
